package com.lenin.omena.client;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.event.ProduceListChangeEvent;
import com.lenin.omena.client.event.ProduceListChangeEventHandler;
import com.lenin.omena.client.event.SaveUserEvent;
import com.lenin.omena.client.event.SaveUserEventHandler;
import com.lenin.omena.client.event.TestEvent;
import com.lenin.omena.client.event.TestEventHandler;
import com.lenin.omena.shared.SaveUserResult;
import com.lenin.omena.shared.domain.Produce;
import com.lenin.omena.shared.domain.User;

public class ProduceManagerPanel extends Composite implements
		ProduceListChangeEventHandler {

	interface MyUiBinder extends UiBinder<Widget, ProduceManagerPanel> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	FlexTable produceTable;
	
	@UiField
	FlexTable addProduceTable;

	@UiField
	HTMLPanel statusPanel;

	private AddProduceRow addProduceRow;
	private HashMap<String, ProduceRow> produceRows;

	public ProduceManagerPanel() {

		initWidget(uiBinder.createAndBindUi(this));

		ProduceListChangeEvent.register(AppleMarket.getEventBus(), this);
		
		produceTable.setStyleName("produceTable");
		addProduceTable.setStyleName("addProduceTable");
		
		produceTable.setHTML(0, 0, "Tuote");
		produceTable.setHTML(0, 1, "Määrä");
		produceTable.setHTML(0, 2, "Hinta");
		produceTable.addCell(0);
		produceTable.addCell(0);
		
		produceTable.getRowFormatter().setStyleName(0, "produceTableHeader");
		
		addProduceRow = new AddProduceRow();
		setAddProduceRow(addProduceRow);

		produceRows = new HashMap<String, ProduceRow>();


	}
	

	public void updateProduceTable(List<Produce> produce) {

		HashMap<String, ProduceRow> newRows = new HashMap<String, ProduceRow>();

		if (produce != null) {
			
			for (Produce p : produce) {

				ProduceRow row = produceRows.get(p.getKey());

				if (row == null) {

					row = new ProduceRow(p);
					addProduceRow(row);

					produceRows.put(p.getKey(), row);

				} else {

					row.setProduce(p);
					row.setFieldValues();

				}

				newRows.put(p.getKey(), row);
				produceRows.remove(p.getKey());

			}
		}

		for (ProduceRow row : produceRows.values()) {
			deleteRow(row);
		}

		produceRows = newRows;
		
		if(produceRows.size() == 0) {
			produceTable.setVisible(false);
		} else {
			produceTable.setVisible(true);
		}

	}

	private void setAddProduceRow(AddProduceRow row) {
		
		addProduceTable.setHTML(0, 0, "Tuote");
		addProduceTable.setHTML(0, 1, "Määrä");
		addProduceTable.setHTML(0, 2, "Hinta");
		addProduceTable.addCell(0);
		addProduceTable.addCell(0);
		
		addProduceTable.getRowFormatter().setStyleName(0, "produceTableHeader");
		
		addProduceTable.setWidget(1, 0, row.getProduceNameField());
		addProduceTable.setWidget(1, 1, row.getProduceAmountField());
		addProduceTable.setWidget(1, 2, row.getProducePriceField());
		addProduceTable.setWidget(1, 3, row.getAddButton());
		addProduceTable.addCell(1);
		
		addProduceTable.getRowFormatter().setStyleName(1, "produceTableAddRow");
		

	}

	private void addProduceRow(ProduceRow row) {

		int insertIndex = produceTable.getRowCount();
		
		produceTable.insertRow(insertIndex);
		
		if(insertIndex == 1) {
			produceTable.getRowFormatter().setStyleName(insertIndex, "produceTableFirstRow");
		}
		
		produceTable.setWidget(insertIndex, 0, row.getProduceNameField());
		produceTable.setWidget(insertIndex, 1, row.getProduceAmountField());
		produceTable.setWidget(insertIndex, 2, row.getProducePriceField());
		produceTable.setWidget(insertIndex, 3, row.getDeleteButton());
		produceTable.setWidget(insertIndex, 4, row.getSaveButton());

	}

	private void deleteRow(ProduceRow row) {

		int rowCount = produceTable.getRowCount();

		for (int i = 1; i < rowCount; i++) {
			if (produceTable.getWidget(i, 0).equals(row.getProduceNameField())) {
				produceTable.removeRow(i);
				break;
			}
		}

	}


	@Override
	public void onProduceListChange(ProduceListChangeEvent event) {

		List<Produce> produce = event.getProduce();

		if (produce != null) {

			setStatusText("Tuote lisätty tai poistettu", "#080");

			updateProduceTable(produce);
			
			addProduceRow.clearFields();
			
		} else {

			setStatusText("Tuotteen lisääminen tai poistaminen epäonnistui.", "#800");

		}

	}


	private void setStatusText(String text, String color) {
		
		statusPanel.getElement().setAttribute("style", "color: "+color);
		statusPanel.getElement().setInnerHTML(text);
		
	}


}
